//
//  ZHFAreaViewController.m
//  GoTraveling
//
//  Created by dllo on 15/12/23.
//  Copyright © 2015年 开发华佗. All rights reserved.
//

#import "ZHFAreaViewController.h"

#import "MBProgressHUD.h"

#import "ZHFAreaCell.h"

#import "Resources.h"

#import "ZHFPlaceViewController.h"

#import "ZHFData.h"

@interface ZHFAreaViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *areaArray;

@property (nonatomic, strong) MBProgressHUD *mbBarPrpgressV;

@end

@implementation ZHFAreaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.areaArray = [NSMutableArray array];
    
    //返回按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_nav_back_button"] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 60) style:UITableViewStylePlain];
    self.tableView.backgroundColor = self.view.backgroundColor;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[ZHFAreaCell class] forCellReuseIdentifier:@"areaCell"];
    [self.view addSubview:self.tableView];
    
    self.tableView.rowHeight = 220 * ADAPT_HEIGHT;
    
    self.mbBarPrpgressV = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.view bringSubviewToFront:self.getDataBtn];
    [self AFNWorking];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.areaArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZHFAreaCell *cell = [tableView dequeueReusableCellWithIdentifier:@"areaCell"];
    cell.backgroundColor = self.view.backgroundColor;
    cell.area = [self.areaArray objectAtIndex:indexPath.row];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    CGRect frame = cell.frame;
    frame.size.height = 220 * ADAPT_HEIGHT;
    cell.frame = frame;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZHFPlaceViewController *placeVC = [[ZHFPlaceViewController alloc] init];
    placeVC.Id = [[self.areaArray objectAtIndex:indexPath.row] Id];
    placeVC.area = [self.areaArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:placeVC animated:YES];
}

#pragma mark - 返回
- (void)leftAction:(UIBarButtonItem *)bar {
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - AFN网络请求
- (void)AFNWorking {
    
    self.getDataBtn.hidden = YES;
    [self.mbBarPrpgressV show:YES];
    NSString *strUrl = [NSString stringWithFormat:@"http://chanyouji.com/api/destinations/%@.json?page=1", self.Id];
    
    [Resources getDataWithName:strUrl block:^(id result) {
        
        NSArray *bigArray = result;
        
        for (NSDictionary *dic in bigArray) {
            ZHFData *area = [ZHFData modelWithDictionary:dic];
            [self.areaArray addObject:area];
        }
        
        [self.mbBarPrpgressV hide:YES];
        [self.tableView reloadData];
        
    } error:^{
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"网络连接错误" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if (self.areaArray.count == 0) {
                self.getDataBtn.hidden = NO;
            }
            [self.mbBarPrpgressV hide:YES];
        }];
        [alert addAction:action];
        
        [self presentViewController:alert animated:YES completion:nil];
    }];
}

#pragma mark 点击刷新
- (void)tapAction:(UITapGestureRecognizer *)button {
    
    [self AFNWorking];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
